package com.example.map_navi_plugin;


import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.kayoxu.androidlib.dialog.common.IBottomSheetDialog;


public class NaviDialog extends LinearLayout {
    private Context context;
    IBottomSheetDialog sheetDialog;
    private LinearLayout rootView;
    private View root;

    public NaviDialog(Context context) {
        super(context);
        initView(context);
    }


    public NaviDialog(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public NaviDialog(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public NaviDialog(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }


    private void initView(Context context) {
        this.context = context;
        root = LayoutInflater.from(context).inflate(R.layout.dialog_navi,
                this, true);
        rootView = root.findViewById(R.id.rootView);
        firstAdd = true;
    }


    boolean firstAdd = true;

    public NaviDialog addView(String name, NaviUtils.NaviResult naviResult) {
        IButton button = new IButton(context);
        button.setLayoutParams(new LayoutParams(getWindowWith(context) - dp2px(context, 50), dp2px(context, 50)));
        button.getBtnText().setText(name);
        rootView.addView(button);
        button.showTopLine(!firstAdd);

        button.setOnClickListener(v -> {
            if (null != naviResult) {
                naviResult.onClick();
                sheetDialog.dismiss();
            }
        });
        firstAdd = false;
        return this;
    }

    public NaviDialog show() {
        if (null == sheetDialog) {
            sheetDialog = new IBottomSheetDialog(context);
            sheetDialog.setContentView(root);
            if (null != sheetDialog.getWindow()) {
                sheetDialog.getWindow().findViewById(R.id.design_bottom_sheet)
                        .setBackgroundResource(android.R.color.transparent);
            }
        }
        sheetDialog.show();

        return this;
    }


    public int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public int getWindowWith(Context activity) {
        if (activity instanceof Activity) {
            WindowManager manager = ((Activity) activity).getWindowManager();
            DisplayMetrics outMetrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(outMetrics);
            int width2 = outMetrics.widthPixels;
            int height2 = outMetrics.heightPixels;
            return width2;
        }
        return 0;
    }

}
